package com.a3dweijing.wetouch2.Activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.InvitelistAdapter;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.ReferralcodeDate;
import com.a3dweijing.wetouch2.constant.Api;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.a3dweijing.wetouch2.network.RetrofitUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class InviteListActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.top_text)
    TextView topText;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    @BindView(R.id.recycle_invite)
    RecyclerView recycleInvite;
    LinearLayoutManager linearLayoutManager;
    List<ReferralcodeDate.ReferralListBean> referalist = new ArrayList<>();
    InvitelistAdapter inviteListAdapter;
    Api api;
    @BindView(R.id.resaut_status)
    LinearLayout resautStatus;
    @BindView(R.id.loading_date)
    LinearLayout loadingDate;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_list);
        ButterKnife.bind(this);

        getData();
        initView();

    }

    private void initView() {
        topText.setText("我的邀请列表");
        context = InviteListActivity.this;
        linearLayoutManager = new LinearLayoutManager(this);
        recycleInvite.setLayoutManager(linearLayoutManager);
        if (inviteListAdapter == null) {
            inviteListAdapter = new InvitelistAdapter(referalist, context);
            recycleInvite.setAdapter(inviteListAdapter);
        }


    }

    private void getData() {
        int id = Integer.parseInt(Singleton.getInstance().getUser().getUser_id());
        api = RetrofitUtil.getInstance().getApiOfPhp();
        Observable<ReferralcodeDate> cData = api.getReferalist(id);
        cData.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
                new Observer<ReferralcodeDate>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposables.add(d);
                        loadingDate.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onNext(ReferralcodeDate refera) {
                        loadingDate.setVisibility(View.GONE);
                        List<ReferralcodeDate.ReferralListBean> list = refera.getReferralList();
                        if (refera.isFlag()) {
                            if (list != null && list.size() > 0) {
                                referalist.clear();
                                referalist.addAll(list);
                            } else {
                                resautStatus.setVisibility(View.VISIBLE);
                            }
                            inviteListAdapter.notifyDataSetChanged();

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        resautStatus.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
